/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package trombinoscope.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.*;
import javax.swing.JOptionPane;
import trombinoscope.modele.*;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;



/**
 *
 * @author Jef
 */
public class servletAuthentification extends HttpServlet {

	public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
        {
	
            try
            {
                String mail = request.getParameter("login");
                String password = request.getParameter("password");
		
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
            
            
                if(UtilisateurDAO.isUserExistant(mail, password))
                {
                    
                    //Récupération des identifiants et stockage dans notre objet de session
                    HttpSession session = request.getSession();
                    session.setAttribute("login", mail);
                    session.setAttribute("password", password);
                    
                    Utilisateur userConnecte = UtilisateurDAO.getUserByMailPassword(mail, password);
            
                    String nom = userConnecte.getNom();
                    String prenom = userConnecte.getPrenom();
                    int idUser = userConnecte.getIdUser();
                   
                  
                    boolean admin = false;
                    
                    if(UtilisateurDAO.isAdmin(idUser))
                    {
                        admin=true;
                    }
                    else
                    {
                        admin=false;
                    }
                    
                    // affichage de la liste des groupes
                    ArrayList<Groupe> listeGroupes;
                    listeGroupes = GroupeDAO.getListeGroupe();
                    
                    
                    // récupèration du contexte de l'application
                    ServletContext context = getServletContext();
                    
                    context.setAttribute("nomUser", nom);
                    context.setAttribute("prenomUser", prenom);
                    context.setAttribute("adminUser", admin);
                    context.setAttribute("idUser",idUser);
                    context.setAttribute("listegroupe", listeGroupes);
                    
                    
                    // création d'un cookie pour le login
                    System.out.println("Création des cookies.");
                    Cookie loginCookie = new Cookie("login", mail);
                    response.addCookie(loginCookie);
                    
                    request.getRequestDispatcher("accueil.jsp").forward(request, response);
                    
                    
                }
                
                else
                {
                   // JOptionPane.showMessageDialog(null, "Authentification échouée!", "Authentification!", JOptionPane.ERROR_MESSAGE);
                    
                   request.getRequestDispatcher("authentification_echouee.jsp").forward(request, response);
                  
                  
                }
            }
            
            
            catch (SQLException e) 
            {	
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<p><strong> Erreur avec la base. </strong>");
		out.println("<p> <a href= \"./authentification.jsp \">Page d'authentification</a> </p>");
		out.println("Cause de l'erreur : " + e.getCause());
		
            }
	}	
      @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {       
            processRequest(request, response);          
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {        
            processRequest(request, response);        
    }    
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
 
   